//
//  XQCommonView.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/1/28.
//

import UIKit

public enum ImageLayoutType {
    case left
    case top
}

public enum ToastPosition {
    case top
    case center
    case bottom
}

class XQCommonView: UIStackView{

    lazy var imageView: UIImageView = {
        let vi = UIImageView()
        vi.contentMode = .scaleAspectFit
        return vi
    }()
    
    lazy var messageLb: UILabel = {
        let lab = UILabel()
        lab.font = .systemFont(ofSize: 14, weight: .bold)
        lab.numberOfLines = 2
        return lab
    }()
    
    public init(message: String? = nil,
                image: UIImage? = nil,
                imageType : ImageLayoutType = .top,
                spaceImage: CGFloat = 5,
                spaceText: CGFloat = 5) {
        
        super.init(frame: CGRect.zero)
        
        axis = imageType == .left ? .horizontal : .vertical
        spacing = image != nil && message != nil ? spaceImage : 0
        alignment = .center
        distribution = .fill
        
        if let image = image{
            imageView.image = image
            NSLayoutConstraint.activate([
                imageView.widthAnchor.constraint(greaterThanOrEqualToConstant: 28),
                imageView.heightAnchor.constraint(greaterThanOrEqualToConstant: 28)
            ])
            addArrangedSubview(imageView)
        }
        if let message = message {
            let vStack = UIStackView()
            vStack.axis = .vertical
            vStack.alignment = .center
            
            messageLb.text = message
            vStack.addArrangedSubview(messageLb)
            
            addArrangedSubview(vStack)
        }

    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
